package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.system.domain.WebUserCardTime;
import com.ruoyi.system.domain.WebUserCardTimeLog;
import com.ruoyi.system.mapper.WebUserCardTimeMapper;
import com.ruoyi.system.service.IWebUserCardTimeLogService;
import com.ruoyi.system.service.IWebUserCardTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 次卡服务Service业务层处理
 *
 * @author wsliupan@126.com
 * @date 2021-06-27
 */
@Service
public class WebUserCardTimeServiceImpl extends ServiceImpl<WebUserCardTimeMapper, WebUserCardTime> implements IWebUserCardTimeService {
    @Autowired
    private WebUserCardTimeMapper webUserCardTimeMapper;

    /**
     * 查询次卡服务
     *
     * @param id 次卡服务ID
     * @return 次卡服务
     */
    @Override
    public WebUserCardTime selectWebUserCardTimeById(Long id) {
        return webUserCardTimeMapper.selectWebUserCardTimeById(id);
    }

    /**
     * 查询次卡服务列表
     *
     * @param webUserCardTime 次卡服务
     * @return 次卡服务
     */
    @Override
    @DataScope(deptAlias = "t1")
    public List<WebUserCardTime> selectWebUserCardTimeList(WebUserCardTime webUserCardTime) {
        return webUserCardTimeMapper.selectWebUserCardTimeList(webUserCardTime);
    }

    /**
     * 新增次卡服务
     *
     * @param webUserCardTime 次卡服务
     * @return 结果
     */
    @Override
    @Transactional
    public int insertWebUserCardTime(WebUserCardTime webUserCardTime) {
        webUserCardTimeMapper.insert(webUserCardTime);

        WebUserCardTimeLog userCardTimeLog = new WebUserCardTimeLog();
        userCardTimeLog.setUserId(webUserCardTime.getUserId());
        userCardTimeLog.setTimeId(webUserCardTime.getId());
        userCardTimeLog.setNumber(webUserCardTime.getCountTimes());
        userCardTimeLog.setMoney(webUserCardTime.getMoney());
        userCardTimeLog.setDeptId(webUserCardTime.getDeptId());
        userCardTimeLog.setResidue(webUserCardTime.getTimes());
        iWebUserCardTimeLogService.save(userCardTimeLog);

//        webUserCardTime.setCreateTime(DateUtils.getNowDate());
//        return webUserCardTimeMapper.insertWebUserCardTime(webUserCardTime);
        return 1;
    }

    /**
     * 修改次卡服务
     *
     * @param webUserCardTime 次卡服务
     * @return 结果
     */
    @Override
    public boolean updateWebUserCardTime(WebUserCardTime webUserCardTime) {
        //webUserCardTime.setUpdateTime(DateUtils.getNowDate());
        //return webUserCardTimeMapper.updateWebUserCardTime(webUserCardTime);
        return this.saveOrUpdate(webUserCardTime);
    }

    /**
     * 删除次卡服务对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public boolean deleteWebUserCardTimeByIds(String ids) {
        //return webUserCardTimeMapper.deleteWebUserCardTimeByIds(Convert.toStrArray(ids));
        return this.removeByIds(Arrays.asList(Convert.toStrArray(ids)));
    }

    /**
     * 删除次卡服务信息
     *
     * @param id 次卡服务ID
     * @return 结果
     */
    @Override
    public boolean deleteWebUserCardTimeById(Long id) {
        //return webUserCardTimeMapper.deleteWebUserCardTimeById(id);
        return this.removeById(id);
    }

    @Transactional
    public boolean consumption(WebUserCardTime webUserCardTime, String orderTime,Long serverOrderId) {
        WebUserCardTime data = this.getById(webUserCardTime.getId());
        if (data == null) {
            throw new BusinessException("数据不存在");
        }
        if(data.getTimes() > 0){
            data.setTimes(data.getTimes() - 1);
            this.saveOrUpdate(data);

            WebUserCardTimeLog userCardTimeLog = new WebUserCardTimeLog();
            userCardTimeLog.setUserId(data.getUserId());
            userCardTimeLog.setTimeId(data.getId());
            userCardTimeLog.setNumber(-1l);
            userCardTimeLog.setOrderTime(orderTime);
            userCardTimeLog.setServerOrderId(serverOrderId);
            userCardTimeLog.setMoney(data.getMoney());
            userCardTimeLog.setDeptId(data.getDeptId());
            userCardTimeLog.setResidue(data.getTimes());
            iWebUserCardTimeLogService.save(userCardTimeLog);
        }

        return true;
    }

    /**
     * 充值
     * @param webUserCardTime
     * @return
     */
    @Transactional
    public boolean chongzhi(WebUserCardTime webUserCardTime){
        WebUserCardTime data = this.getById(webUserCardTime.getId());
        if(data == null){
            throw new BusinessException("未获取到次卡数据");
        }
        if(webUserCardTime.getTimes()== null){
            throw new BusinessException("未获取到充值次数");
        }
        data.setTimes(data.getTimes()+webUserCardTime.getTimes());
        data.setCountTimes(data.getCountTimes()+webUserCardTime.getTimes());
        this.saveOrUpdate(data);

        WebUserCardTimeLog userCardTimeLog = new WebUserCardTimeLog();
        userCardTimeLog.setUserId(data.getUserId());
        userCardTimeLog.setTimeId(data.getId());
        userCardTimeLog.setNumber(webUserCardTime.getTimes());
        userCardTimeLog.setMoney(data.getMoney());
        userCardTimeLog.setDeptId(data.getDeptId());
        userCardTimeLog.setResidue(data.getCountTimes());
        iWebUserCardTimeLogService.save(userCardTimeLog);

        return true;
    }

    /**
     * 查询用户的总次数（小程序）
     * @param userId
     * @return
     */
    public Map<String,Object> selectUserData(Long userId){
        return webUserCardTimeMapper.selectUserData(userId);
    }

    @Autowired
    private IWebUserCardTimeLogService iWebUserCardTimeLogService;
}
